(function($,document){

	var dataHistory;

	/**
	 * 历史数据对象
	 * @param {Object} name	数据名称
	 * @memberOf {TypeName}
	 */
	function DataHistory(name){
		this.name = name;
		this.size = 0;		//当前长度
		this.limit = 6;		//最大长度
		this.list = {};		//数据内容
	}

	/**
	 * 去除空白字符
	 * @param {Object} s
	 * @return {TypeName}
	 */
	function trim(s){
		return s.replace( /^[ \t]*|[ \t]*$/g, "");
	}

	/**
	 * 从指定Cookie中取得指定数据名称的数据后，存储到数据对象中
	 * @param {Object} cookieName
	 * @param {Object} dataName
	 */
	function loadFromCookie(cookieName,dataName){
		var cookies = document.cookie.split('; ')
		for(var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++){
			if(trim(parts.shift()) === cookieName){
				var values = parts[0].split(';');
				for(var j = 0, value; (value = values[j] && unescape(values[j]).split('=')); j++){
					if(trim(value.shift()) === dataName){
						var data = unescape(value[0]).split(';');
						dataHistory = new DataHistory(dataName);
						for(var i = 0; i<data.length;i++){
							if(data[i] != ''){
								dataHistory.list[data[i]] = dataHistory.size;
								dataHistory.size++;
							}
						}
					}
				}
				break;
			}
		}
	}

	/**
	 * 将数据对象的list里的值存储到cookie中去
	 * @param {Object} cookieName
	 * @param {Object} dataName
	 */
	function store2Cookie(cookieName,dataName){

		if(dataHistory.name === dataName){
			//如果数据对象list为空时，删除cookie
			if(dataHistory.size <= 0){
				$.removeCookie(cookieName);
			}else{
				var store = dataName + '=';
				for(var entry in dataHistory.list){
					store += escape(entry + ';');
				}

				var expires = new Date();
				expires.setFullYear(expires.getFullYear() + 1);

				document.cookie = [
					cookieName + '=',
					escape(store),
					'; expires=' + expires.toGMTString()
				].join('');
			}
		}

	}

	/**
	 * 从数据对象的list中移除最早的一条记录
	 */
	function shiftFromList(){
		for(var entry in dataHistory.list){
			if(dataHistory.list[entry] == 0){
				delete dataHistory.list[entry];
			}else{
				dataHistory.list[entry]--;
			}
		}
	}

	/**
	 * 从指定的Cookie里指定的数据名称中移除指定的数据
	 * @param {Object} cookieName
	 * @param {Object} dataName
	 * @param {Object} data
	 */
	$.removeDataFromCookie = function(cookieName,dataName,data){
		if(!data){
			return false;
		}

		loadFromCookie(cookieName,dataName);

		if(typeof dataHistory == 'undefined'){
			return false;
		}

		var data = trim(data);
		if(data in dataHistory.list){
			var index = dataHistory.list[data];
			delete dataHistory.list[data];
			dataHistory.size--;

			store2Cookie(cookieName,dataName);
		}else{
			return false;
		}

	}

	/**
	 * 向指定的Cookie里指定的数据名称中添加数据
	 * @param {Object} cookieName	cookie名称
	 * @param {Object} dataName		需要添加数据的名称
	 * @param {Object} data			数据
	 * @return {TypeName}
	 */
	$.addData2Cookie = function(cookieName,dataName,data){
		if(!data){
			return false;
		}

		var data = trim(data);

		loadFromCookie(cookieName,dataName);

		if(typeof dataHistory == 'undefined'){
			dataHistory = new DataHistory(dataName);
		}else{

			if(data in dataHistory.list){
				return false;
			}

			if(dataHistory.size == dataHistory.limit){
				shiftFromList();
			}

		}

		dataHistory.list[data] = dataHistory.size;
		dataHistory.size++;

		store2Cookie(cookieName,dataName);

	}

	/**
	 * 从指定Cookie中取出指定名称的数据
	 * @param {Object} cookieName	cookie名称
	 * @param {Object} dataName		需要取出数据的名称
	 * @return {TypeName}
	 */
	$.getDataFromCookie = function(cookieName,dataName){

		//如果数据对象不存在，从cookie中加载数据
		if(typeof dataHistory == 'undefined'){
			loadFromCookie(cookieName,dataName);
		}
		//如果从Cookie中也无法加载到数据，说明Cookie不存在，返回空数据
		if(dataHistory){
			return dataHistory.list;
		}else{
			return null;
		}
	}

	/**
	 * 删除指定名称的Cookie
	 * @param {Object} cookieName
	 */
	$.removeCookie = function(cookieName){
		document.cookie = [
			cookieName + '=',
			' ',
			'; expires=-1'
		].join('');
	}

})(jQuery,document);